﻿using System;
using System.Linq;
using System.Web.Services;
using Dost.Khcn.Biz;
using Dost.Khcn.Web.Common;

namespace Dost.Khcn.Web.DanhMuc.LinhVuc
{
    public partial class List : System.Web.UI.Page
    {
        #region Properites

        private static LinhVucBiz _linhVucBiz;
        private static LinhVucBiz LinhVucManager
        {
            get { return _linhVucBiz ?? (_linhVucBiz = new LinhVucBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!Page.IsPostBack)
            {
                var nhomLinhVucs = LinhVucManager.GetAllNhomLinhVucs();
                //foreach (var nhomLinhVuc in nhomLinhVucs)
                //{
                //    nhomLinhVuc.Ten = string.Format("{0}{1}", string.IsNullOrEmpty(nhomLinhVuc.Ma) ? string.Empty : nhomLinhVuc.Ma + "-", nhomLinhVuc.Ten);
                //}
                nhomLinhVucs.Insert(0, new Data.EntityModel.NhomLinhVuc { Id = 0, Ten = string.Empty });

                ddlNhomLinhVucs.DataTextField = "MaTen";
                ddlNhomLinhVucs.DataValueField = "Id";
                ddlNhomLinhVucs.DataSource = nhomLinhVucs;
                ddlNhomLinhVucs.DataBind();
            }
        }

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object LinhVucList(int jtStartIndex, int jtPageSize, string jtSorting, string name, string code, int nhomLinhVucId)
        {
            return LinhVucManager.GetLinhVucs(jtStartIndex, jtPageSize, jtSorting, name, code, nhomLinhVucId);
        }

        [WebMethod(EnableSession = true)]
        public static object Delete(int Id)
        {
            return LinhVucManager.DeleteLinhVuc(Id);
        }

        [WebMethod(EnableSession = true)]
        public static object Create(Data.EntityModel.LinhVuc record)
        {
            try
            {
                var addedRecord = LinhVucManager.CreateLinhVuc(record);
                return new { Result = "OK", Record = addedRecord };
            }
            catch (ApplicationException appEx)
            {
                var data = appEx.Message == "CodeIsExisted" ? record.Ma : record.Ten;
                return new { Result = "ERROR", Message = string.Format(WebCommon.GetGlobalString(appEx.Message), data) };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object Update(Data.EntityModel.LinhVuc record)
        {
            try
            {
                LinhVucManager.UpdateLinhVuc(record);
                return new { Result = "OK" };
            }
            catch (ApplicationException appEx)
            {
                var data = appEx.Message == "CodeIsExisted" ? record.Ma : record.Ten;
                return new { Result = "ERROR", Message = string.Format(WebCommon.GetGlobalString(appEx.Message), data) };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object GetAll()
        {
            var linhVucs = LinhVucManager.GetAllLinhVucs();
            linhVucs.Insert(0, new Data.EntityModel.LinhVuc { Id = 0, Ten = string.Empty, NhomLinhVucId = 0});

            return linhVucs.Select(x => new { x.Id, x.MaTen, x.NhomLinhVucId });
        }

        [WebMethod(EnableSession = true)]
        public static object GetOptions()
        {
            var linhVucs = LinhVucManager.GetAllLinhVucs();
            linhVucs.Insert(0, new Data.EntityModel.LinhVuc { Id = 0, Ten = string.Empty });

            return new { Result = "OK", Options = linhVucs.Select(x => new { DisplayText = x.Ten, Value = x.Id }) };
        }

        [WebMethod(EnableSession = true)]
        public static object NhomLinhVucList()
        {
            var nhomLinhVucs = LinhVucManager.GetAllNhomLinhVucs();

            nhomLinhVucs.Insert(0, new Data.EntityModel.NhomLinhVuc { Id = 0, Ten = string.Empty });
            return new { Result = "OK", Options = nhomLinhVucs.Select(c => new { DisplayText = c.MaTen, Value = c.Id }) };
        }

        #endregion
    }
}